﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.Concurrent;
using UtilPublic.ServerLoger;
using System.Threading;
using System.Linq;

namespace UtilPublic.ApiDispatcher
{
    #region string - action()
    /// <summary>
    /// 属性字符串
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ApiDpPubSA0Attribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public string Uid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        public ApiDpPubSA0Attribute(string uid) { Uid = uid; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="desc"></param>
        public ApiDpPubSA0Attribute(string uid, string desc) { Uid = uid; Desc = desc; }
    }
    /// <summary>
    /// 
    /// </summary>
    public static partial class ApiDpPubMgr
    {
        /// <summary>
        /// 获取控制器 Action参数0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static Dictionary<string, Action> GetControllerS<T>(T t)
        {
            var result = new Dictionary<string, Action>();
            if (t != null)
            {
                Type type = t.GetType();
                var methods = type.GetMethods();
                foreach (var method in methods)
                {
                    var atts = method.GetCustomAttributes(typeof(ApiDpPubSA0Attribute), true);
                    if (atts.Length > 0)
                    {
                        var att = ((ApiDpPubSA0Attribute)atts[0]);
                        var Act = (Action)Delegate.CreateDelegate(typeof(Action), t, method);
                        result[att.Uid] = Act;
                    }
                }
            }
            return result;
        }
    }
    #endregion
    #region string - action(T)
    /// <summary>
    /// 属性字符串
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ApiDpPubSA1Attribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public string Uid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        public ApiDpPubSA1Attribute(string uid) { Uid = uid; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="desc"></param>
        public ApiDpPubSA1Attribute(string uid, string desc) { Uid = uid; Desc = desc; }
    }
    /// <summary>
    /// 指令控制器
    /// </summary>
    public static partial class ApiDpPubMgr
    {
        /// <summary>
        /// 获取控制器 Action参数1
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="t"></param>
        public static Dictionary<string, Action<T1>> GetControllerS<T, T1>(T t)
        {
            var result = new Dictionary<string, Action<T1>>();
            if (t != null)
            {
                Type type = t.GetType();
                var methods = type.GetMethods();
                foreach (var method in methods)
                {
                    var atts = method.GetCustomAttributes(typeof(ApiDpPubSA1Attribute), true);
                    if (atts.Length > 0)
                    {
                        var att = ((ApiDpPubSA1Attribute)atts[0]);
                        var Act = (Action<T1>)Delegate.CreateDelegate(typeof(Action<T1>), t, method);
                        result[att.Uid] = Act;
                    }
                }
            }
            return result;
        }
    }
    #endregion

    #region int - action()
    /// <summary>
    /// 属性字符串
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ApiDpPubIA0Attribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public int Uid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        public ApiDpPubIA0Attribute(int uid) { Uid = uid; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="desc"></param>
        public ApiDpPubIA0Attribute(int uid, string desc) { Uid = uid; Desc = desc; }
    }
    /// <summary>
    /// 
    /// </summary>
    public static partial class ApiDpPubMgr
    {
        /// <summary>
        /// 获取控制器 Action参数0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static Dictionary<int, Action> GetControllerI<T>(T t)
        {
            var result = new Dictionary<int, Action>();
            if (t != null)
            {
                Type type = t.GetType();
                var methods = type.GetMethods();
                foreach (var method in methods)
                {
                    var atts = method.GetCustomAttributes(typeof(ApiDpPubIA0Attribute), true);
                    if (atts.Length > 0)
                    {
                        var att = ((ApiDpPubIA0Attribute)atts[0]);
                        var Act = (Action)Delegate.CreateDelegate(typeof(Action), t, method);
                        result[att.Uid] = Act;
                    }
                }
            }
            return result;
        }
    }
    #endregion
    #region int - action(T)
    /// <summary>
    /// 属性字符串
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ApiDpPubIA1Attribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public int Uid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        public ApiDpPubIA1Attribute(int uid) { Uid = uid; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="desc"></param>
        public ApiDpPubIA1Attribute(int uid, string desc) { Uid = uid; Desc = desc; }
    }
    /// <summary>
    /// 指令控制器
    /// </summary>
    public static partial class ApiDpPubMgr
    {
        /// <summary>
        /// 获取控制器 Action参数1
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="t"></param>
        public static Dictionary<int, Action<T1>> GetControllerI<T, T1>(T t)
        {
            var result = new Dictionary<int, Action<T1>>();
            if (t != null)
            {
                Type type = t.GetType();
                var methods = type.GetMethods();
                foreach (var method in methods)
                {
                    var atts = method.GetCustomAttributes(typeof(ApiDpPubIA1Attribute), true);
                    if (atts.Length > 0)
                    {
                        var att = ((ApiDpPubIA1Attribute)atts[0]);
                        var Act = (Action<T1>)Delegate.CreateDelegate(typeof(Action<T1>), t, method);
                        result[att.Uid] = Act;
                    }
                }
            }
            return result;
        }
    }
    #endregion

}